package com.goldmantis.alitrip.controller.system;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.UserThreadLocal;
import com.goldmantis.alitrip.common.annotation.Log;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.pojo.SysUser;
import com.goldmantis.alitrip.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "用户管理")
@RequestMapping("sys/user")
@RequiresAuthentication

public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取用户
     * @param searchEntity
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "用户列表",httpMethod = "GET")
    @Log(operationName = "用户列表", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> list(
                                              @ApiParam("查询对象") SysUser searchEntity,
                                              @ApiParam("当前页") @RequestParam(defaultValue = "1") Integer offset,
                                              @ApiParam("每页显示数量") @RequestParam(defaultValue = "10")Integer limit) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.pageList(searchEntity,offset,limit)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


    /**
     * 导出用户信息excel
     * @param searchEntity
     * @return
     */
    @GetMapping(value = "/exceltodata")
    @ApiOperation(value = "用户列表",httpMethod = "GET")
    @Log(operationName = "用户列表", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> exceltodata(
            @ApiParam("查询对象") SysUser searchEntity) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.exceltodata(searchEntity)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


    @GetMapping(value = "{userId}")
    @ApiOperation(value = "根据Id获取用户",httpMethod = "GET")
    @Log(operationName = "根据id查询用户", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键",required = true) @PathVariable(required = true) Long userId) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.selectById(userId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增用户",httpMethod = "POST")
    @Log(operationName = "新增用户", logCatagory = EnumLogCatagory.operation, operationType = EnumOperationType.add)
    public ResponseEntity<ReturnMessage> post(@ApiParam("用户对象") @RequestBody @Valid SysUser sysUser) {
        try {
            sysUser.setCreatetime(new Date());
            sysUser.setCreateBy(UserThreadLocal.get().getUserId());
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.insert(sysUser)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 编辑用户
     * @param sysUser
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑用户",httpMethod = "PUT")
    @Log(operationName = "编辑用户", logCatagory = EnumLogCatagory.operation, operationType = EnumOperationType.edit)
    public ResponseEntity<ReturnMessage> put(@ApiParam("用户对象") @RequestBody SysUser sysUser) {
        try {
            sysUser.setCreatetime(new Date());
            sysUser.setCreateBy(UserThreadLocal.get().getUserId());
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.update(sysUser)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除用户
     * @param user_ids
     * @return
     */
    @DeleteMapping(value = "{user_ids}")
    @Log(operationName = "批量删除用户", logCatagory = EnumLogCatagory.operation, operationType = EnumOperationType.delete)
    @ApiOperation(value = "批量删除用户",httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("用户id 逗号隔开（1,2）") @PathVariable String user_ids){
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(user_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysUserService.deleteByIds(ids,SysUser.class,"userid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


}
